/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.biz;

import com.myapp.entities.Question;
import com.myapp.entities.Survey;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author BAOBAO
 */
public class ReportBiz {

    public ArrayList<Survey> getSurveys(String realPath, String loadFileXml) {

        try {
            ArrayList<Survey> arr = new ArrayList<Survey>();
            NodeList surNode = this.searchSurvey(realPath, loadFileXml, "");
            NodeList quesNode = this.searchSurvey(realPath, loadFileXml, "/question/question-content");

            Question ques = new Question();
            for (int j = 0; j < quesNode.getLength(); j++) {
                Element elmQues = (Element) surNode.item(j);
                ques.setId(elmQues.getAttribute("id"));
                ques.setQuestion(elmQues.getTextContent());

            }
            Survey survey;
            NodeList lstAns;

            for (int i = 0; i < surNode.getLength(); i++) {
                survey = new Survey();
                Element elmSur = (Element) surNode.item(i);
                Element elmQues = (Element) quesNode.item(i);

                survey.setSurveyId(elmSur.getAttribute("id"));
                survey.setSurveyName(elmSur.getAttribute("name"));
                survey.setContent(elmQues.getTextContent());
                ArrayList<String> answer = new ArrayList<String>();
                ArrayList<String> statis = new ArrayList<String>();
                int total = 0;
                lstAns = this.searchSurvey(realPath, loadFileXml, "/question[@id='" + (i + 1) + "']/answer");
                for (int j = 0; j < lstAns.getLength(); j++) {

                    answer.add(lstAns.item(j).getTextContent());
                    Element element = (Element) lstAns.item(j);
                    statis.add(element.getAttribute("statistic"));
                    total += Integer.parseInt(element.getAttribute("statistic"));

                }
                survey.setTotal(total + "");
                survey.setAnsList(answer);
                survey.setStatistic(statis);

                arr.add(survey);
            }
            return arr;
        } catch (Exception ex) {

            ex.printStackTrace();
            return null;
        }

    }

    public NodeList searchSurvey(String realPath, String loadFileXml, String query) {

        try {
            String expression = "//surveys/survey" + query;
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            String path = realPath + loadFileXml;
            Document doc = builder.parse(new File(path));
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
            return nodes;


        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }

    }

    public static void main(String[] args) {

        ReportBiz rp = new ReportBiz();
        //JOptionPane.showMessageDialog(null,rp.searchSurvey("C:\\Users\\BAOBAO\\Desktop\\SLA-IJX\\SuperSurvey\\web\\", "surveys.xml","").getLength());
//         ArrayList<Survey> arr=new ArrayList<Survey>();
//         arr=rp.getSurveys("C:\\Users\\BAOBAO\\Desktop\\SLA-IJX\\SuperSurvey\\web\\","surveys.xml");
//         ArrayList<String> lst=new ArrayList<String>();
//         for (int i = 0; i < arr.size(); i++) {
//             System.out.println(Integer.parseInt(arr.get(i).getTotal()));
//         }


//         NodeList lstAns=rp.searchSurvey("C:\\Users\\BAOBAO\\Desktop\\SLA-IJX\\SuperSurvey\\web\\", "surveys.xml","/question[@id='1']/answer");
//         for (int i = 0; i < lstAns.getLength(); i++) {
//             Element element = (Element) lstAns.item(i);
//              JOptionPane.showMessageDialog(null,element.getAttribute("statistic"));
//         }
        double E1 = 0.7272727272 ;
        
        DecimalFormat dec = new DecimalFormat("###.#");

        System.out.println(dec.format(E1));
        JOptionPane.showMessageDialog(null, dec.format(E1));
    }
}
